﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMAS.DAL;
using HLVControl.Grid;
using HLVControl.Grid.Data;
using APP.QUANLY.Common;

namespace RMAS
{
    public partial class usrc_ListWareProduct : UserControl
    {
        bool KQ = false;
        public usrc_ListWareProduct()
        {
            InitializeComponent();
        }
        private void usrc_ListWareProduct_Load(object sender, EventArgs e)
        {
            LoadAll();
        }
        public void LoadAll()
        {
            LoadToTreeview(treeview, StaticVar.DanhSachTonKhoSanPham);
            UIHelper.LoadFocus(txtTimKiem);
        }
        public void LoadData()
        {
            LoadToCmb_WareHouse();
        }
        private void LoadToCmb_WareHouse()
        {
            try
            {
                DM_WAREHOUSEList list = StaticVar.DanhSachKhoSP;
                cmbWareHouse.DataSource = null;

                if (list.Count == 0)
                {
                    if (MessageBox.Show("Chưa có danh sách kho!\nVui lòng thêm kho trước!\nBạn có muốn thêm kho không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        frmDM_WareHouse Ware = new frmDM_WareHouse(null, EnumGUI.ThaoTac.them);
                        if (Ware.ShowDialog() == DialogResult.OK) list = StaticVar.DanhSachKhoSP;
                    }
                }
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(string));
                data.Columns.Add("Kho hàng", typeof(string));
                foreach (DM_WAREHOUSE obj in list)
                {
                    data.Rows.Add(obj.WHID, obj.WHName);
                }

                cmbWareHouse.DataSource = data;
                cmbWareHouse.DisplayMember = "Kho hàng";
                cmbWareHouse.ValueMember = "Mã";
                cmbWareHouse.ColumnWidths = "0,200";
                cmbWareHouse.SelectedIndex = -1;
                cmbWareHouse.Text = "";
            }
            catch { }
        }
        private void LoadToCmb_Year(string WareID)
        {
            try
            {
                cmbYear.DataSource = null;
                
                if (StaticVar.DanhSachTonKhoSanPham.Count > 0)
                {
                    WAREHOUSE_PRODUCT_Provider _prov = new WAREHOUSE_PRODUCT_Provider();
                    //_prov.Cmd_LoadByCommandText("SELECT [Year] FROM WAREHOUSE_PRODUCT WHERE WHID='' GROUP BY [Year]");
                    DataTable data = new DataTable();
                    data.Columns.Add("Year", typeof(string));
                    
                    foreach (WAREHOUSE_PRODUCT obj in StaticVar.DanhSachTonKhoSanPham)
                    {
                        data.Rows.Add(obj.Year);
                    }

                    cmbWareHouse.DataSource = data;
                    cmbWareHouse.DisplayMember = "Kho hàng";
                    cmbWareHouse.ValueMember = "Mã";
                    cmbWareHouse.ColumnWidths = "0,200";
                    cmbWareHouse.SelectedIndex = -1;
                    cmbWareHouse.Text = "";
                }
            }
            catch { }
        }
        private void LoadToTreeview(TreeListView treeview, WAREHOUSE_PRODUCTList list)
        {
            try
            {
                
                treeview.Rows.Clear();
                int count = 1;
                if (list == null) return;
                foreach (WAREHOUSE_PRODUCT obj in list)
                {
                    TreeListRow row = treeview.CreateRow();
                    row.Cells.Add(new TreeListCell(count));
                    row.Cells.Add(new TreeListCell(obj.PrID));
                    row.Cells.Add(new TreeListCell(obj.WHID));
                    row.Cells.Add(new TreeListCell(obj.PrName));
                    row.Cells.Add(new TreeListCell(obj.FirstBalance));
                    row.Cells.Add(new TreeListCell(obj.LastBalance));
                    row.Cells.Add(new TreeListCell(obj.addUp));
                    row.Cells.Add(new TreeListCell(obj.addDown));
                    treeview.Rows.Add(row);
                    count++;
                }
                treeview.Refresh();
            }
            catch { }
        }
        private void XuLy(EnumGUI.ThaoTac thaotac)
        {
            try
            {
                switch (thaotac)
                {
                    case EnumGUI.ThaoTac.nhapexcel:

                        break;
                    case EnumGUI.ThaoTac.xuatexcel:
                        if (treeview.Rows.Count > 0)
                        {
                            frmExportExcel export = new frmExportExcel("DS_TonKhoSP", treeview);
                            if (export.ShowDialog() == DialogResult.OK)
                            {
                                KQ = true;
                            }
                        }
                        break;
                    case EnumGUI.ThaoTac.timkiem:
                        SearchProvider.SmartSearch(treeview, txtTimKiem.Text.Trim(), new List<string> { });
                        break;
                }
                if (KQ) LoadToTreeview(treeview, StaticVar.DanhSachTonKhoSanPham);
            }
            catch { }
        }
        private void btn_Click(object sender, EventArgs e)
        {
            KQ = false;
            Button btn = (Button)sender;
            string thaotac = btn.Tag.ToString();

            switch (thaotac)
            {
                case "timkiem":
                    XuLy(EnumGUI.ThaoTac.timkiem);
                    break;
                case "nhapexcel":
                    XuLy(EnumGUI.ThaoTac.nhapexcel);
                    break;
                case "xuatexcel":
                    XuLy(EnumGUI.ThaoTac.xuatexcel);
                    break;
            }
        }

        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if (txt.Name == txtTimKiem.Name)
                UIHelper.txt_KeyPress(sender, btnTimKiem, e);
        }
    }
}
